package com.cr.backbone.hibenate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.cr.backbone.util.UtilsDateTime;

/**
 * @project RaedNote
 * @version 1.0
 * @company 
 * @fileDescribe
 * @fileType QueryParam QueryParam.java
 * @author xianghua.hu
 * @date 2012-2-26 下午05:14:27
 */

/**
 * action - hibernate 查询 参数转化工具对象
 * 
 * query['eq_name_s'] 例如： 查询 User{ private String name; } 对象中的name
 * query['eq_userJob.title_s'] 例如： 查询 User{ private Job userJob; } 对象中的Job对象 Job{ private String title} 中的title属性
 * 
 * 记录页面传递过来的查询信息，其中key的含义：操作_hql属性名_类型 
 * 支持的操作： 
 * eq = EQUAL(=)
 * lk = LIKE(like) 
 * gt = GREAT(>) 
 * lt = LESS(<) 
 * ge = GREAT_EQUAL(>=) 
 * le = LESS_EQUAL(<=) 
 * nu = IS_NULL(is null) 
 * 
 * ne = NOT_EQUAL(!=) 
 * nn = NOT_NULL(is not null) 
 * 
 * 
 * 支持的类型： 
 * s = 字符串string：String 
 * b = 逻辑boolean：Boolean 
 * i = 整数int：Integer 
 * l = 长整数long：Long 
 * f = 浮点float：Float 
 * d = 双浮点double：Double 
 * D = 日期date：Date(格式：yyyy-MM-dd) 
 * t = 时间datetime：Date(格式：yyyy-MM-dd hh:mm:ss) 
 * 
 */
public class QueryParam {
	private Map<String, String> query = new HashMap<String, String>();
	private Map<String, QueryParamElment> queryCriteriaParam = new HashMap<String, QueryParamElment>();
	private Map<String, OrderElement> order = new HashMap<String, OrderElement>();

	public Map<String, String> getQuery() {
		return query;
	}

	public void setQuery(Map<String, String> query) {
		this.query = query;
	}

	/**
	 * 获取hibernate criteria param For query
	 * 
	 * @methodDescribe
	 * @return
	 * @author xianghua.hu
	 * @date 2012-2-26 下午09:39:23
	 */
	public Map<String, QueryParamElment> getQueryCriteriaParam() {
		queryCriteriaParam = getQueryCriteriaParamByQuery(getQuery());
		return queryCriteriaParam;
	}

	/**
	 * action query Update To query criteria param
	 * 
	 * @methodDescribe
	 * @param map
	 * @return
	 * @author xianghua.hu
	 * @date 2012-2-26 下午09:47:03
	 */
	private Map<String, QueryParamElment> getQueryCriteriaParamByQuery(
			Map<String, String> map) {
		for (String key : map.keySet()) {
			String value = map.get(key);

			if (key == null || key.equals("")) {
				// if(logger.isDebugEnabled()){
				// logger.debug("由于key=null，添加查询条件失败！value="+value);
				// }
				continue;
			}
			if (value == null || value.equals("")) {
				removeQuery(key);
				// changed = true;
				// if(logger.isDebugEnabled()){
				// logger.debug("由于value=null，删除一个查询条件!key="+key);
				// }
			} else {
				this.queryCriteriaParam.put(key, new QueryParamElmentImpl(key,
						value));
				// changed = true;
				// if(logger.isDebugEnabled()){
				// logger.debug("添加查询条件!key="+key+";value="+value);
				// }
			}
		}
		return queryCriteriaParam;
	}

	/**
	 * hibernate criteria query param
	 * 
	 * @methodDescribe
	 * @param key
	 * @author xianghua.hu
	 * @date 2012-2-26 下午09:44:02
	 */
	private void removeQuery(String key) {
		queryCriteriaParam.remove(key);
	}

	public void setQueryCriteriaParam(
			Map<String, QueryParamElment> queryCriteriaParam) {
		this.queryCriteriaParam = queryCriteriaParam;
	}
	
	/**
	 * 手工填充queryCriteriaParam
	 * @methodDescribe
	 * @param key
	 * @param value
	 * @author xianghua.hu
	 * @date 2012-3-6 下午05:41:38
	 */
	public void setQueryCriteriaParamByKeyAndValue(String key,String value) {
		QueryParamElment valueElement=new QueryParamElmentImpl(key,value);
		this.queryCriteriaParam.put(key, valueElement);
	}

	public Map<String, OrderElement> getOrder() {
		return order;
	}

	public void setOrder(Map<String, OrderElement> order) {
		this.order = order;
	}

	/**
	 * 添加asc排序
	 * 根据当前查询Object 对象的property Asc排序查询
	 * 例如 User.name
	 * 	propertyName = "name";
	 * @methodDescribe
	 * @param propertyName
	 * @return
	 * @author xianghua.hu
	 * @date 2012-2-26 下午09:31:22
	 */
	public QueryParam addOrderAsc(String propertyName) {
		boolean isDesc = false;
		OrderElement OrderElement = new OrderElement(isDesc, propertyName);
		order.put(propertyName, OrderElement);
		return this;
	}

	/**
	 * 添加desc排序
	 * 根据当前查询Object 对象的property Desc排序查询
	 * 例如 User.name
	 * 	propertyName = "name";
	 * @methodDescribe
	 * @param propertyName
	 * @return
	 * @author xianghua.hu
	 * @date 2012-2-26 下午09:31:50
	 */
	public QueryParam addOrderDesc(String propertyName) {
		boolean isDesc = true;
		OrderElement OrderElement = new OrderElement(isDesc, propertyName);
		order.put(propertyName, OrderElement);
		return this;
	}

	
	/**
	 * 通过java代码手工添加查询添加，自动将数据类型String转化为String
	 * 并封装查询参数对象QueryParamElment
	 * @methodDescribe
	 * @param key
	 * @param value
	 * @return
	 * @author xianghua.hu
	 * @date 2012-3-6 下午05:45:56
	 */
	public QueryParam addQuery(String key,String value) {
		setQueryCriteriaParamByKeyAndValue(key, value);
		return this;
	}
	
	/**
	 * 通过java代码手工添加查询添加，自动将数据类型Integer转化为String
	 * 并封装查询参数对象QueryParamElment
	 * @methodDescribe
	 * @param key
	 * @param value
	 * @return
	 * @author xianghua.hu
	 * @date 2012-3-6 下午05:45:56
	 */
	public QueryParam addQuery(String key,Integer value) {
		setQueryCriteriaParamByKeyAndValue(key,String.valueOf(value));
		return this;
	}
	
	/**
	 * 通过java代码手工添加查询添加，自动将数据类型Long转化为String
	 * 并封装查询参数对象QueryParamElment
	 * @methodDescribe
	 * @param key
	 * @param value
	 * @return
	 * @author xianghua.hu
	 * @date 2012-3-6 下午05:45:56
	 */
	public QueryParam addQuery(String key,Long value) {
		setQueryCriteriaParamByKeyAndValue(key,String.valueOf(value));
		return this;
	}
	
	/**
	 * 通过java代码手工添加查询添加，自动将数据类型Float转化为String
	 * 并封装查询参数对象QueryParamElment
	 * @methodDescribe
	 * @param key
	 * @param value
	 * @return
	 * @author xianghua.hu
	 * @date 2012-3-6 下午05:45:56
	 */
	public QueryParam addQuery(String key,Float value) {
		setQueryCriteriaParamByKeyAndValue(key,String.valueOf(value));
		return this;
	}
	
	/**
	 * 通过java代码手工添加查询添加，自动将数据类型Double转化为String
	 * 并封装查询参数对象QueryParamElment
	 * @methodDescribe
	 * @param key
	 * @param value
	 * @return
	 * @author xianghua.hu
	 * @date 2012-3-6 下午05:45:56
	 */
	public QueryParam addQuery(String key,Double value) {
		setQueryCriteriaParamByKeyAndValue(key,String.valueOf(value));
		return this;
	}
	
	/**
	 * 通过java代码手工添加查询添加，自动将数据类型Boolean转化为String
	 * 并封装查询参数对象QueryParamElment
	 * @methodDescribe
	 * @param key
	 * @param value
	 * @return
	 * @author xianghua.hu
	 * @date 2012-3-6 下午05:45:56
	 */
	public QueryParam addQuery(String key,Boolean value) {
		setQueryCriteriaParamByKeyAndValue(key,String.valueOf(value));
		return this;
	}
	
	/**
	 * 通过java代码手工添加查询添加，自动将数据类型Date转化为String
	 * 并封装查询参数对象QueryParamElment
	 * @methodDescribe
	 * @param key
	 * @param value
	 * @return
	 * @author xianghua.hu
	 * @date 2012-3-6 下午05:45:56
	 */
	public QueryParam addQuery(String key,Date value) {
		return addQuery(key,value, "yyyy-MM-dd");
	}
	public QueryParam addQuery(String key,Date value,String pattern) {
		setQueryCriteriaParamByKeyAndValue(key,UtilsDateTime.format(value,pattern));
		return this;
	}

	class OrderElement {
		private boolean isDesc = false;
		private String propertyName;

		public OrderElement(boolean isDesc, String propertyName) {
			super();
			this.isDesc = isDesc;
			this.propertyName = propertyName;
		}

		public OrderElement() {
			super();
		}

		public boolean isDesc() {
			return isDesc;
		}

		public void setDesc(boolean isDesc) {
			this.isDesc = isDesc;
		}

		public String getPropertyName() {
			return propertyName;
		}

		public void setPropertyName(String propertyName) {
			this.propertyName = propertyName;
		}

	}
}
